
package de.godlike.android.data;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class DbHelper {

    private static final String SERVER_TABLE_NAME = "server";

    private SQLiteDatabase db = null;

    // Statements

    public DbHelper(Context context) {
        OpenHelper helper = new OpenHelper(context);
        this.db = helper.getWritableDatabase();
    }

    public void addServer(String url, String user, String password) {
        db.execSQL("INSERT INTO " + SERVER_TABLE_NAME + "(URL, USER, PASSWORD) VALUES ('" + url
                + "','" + user + "','" + password + "'" + ")");
    }

    public void removeServer(String id) {
        db.execSQL("DELETE FROM " + SERVER_TABLE_NAME + " WHERE _id == "+id);
    }
    
    public Cursor getAllServer() {
        return db.query(SERVER_TABLE_NAME, new String[] {
                "_id", "URL", "USER", "PASSWORD"
        }, null, null, null, null, null);
    }

    private static class OpenHelper extends SQLiteOpenHelper {

        private static final int DATABASE_VERSION = 2;

        private static final String DATABASE_NAME = "serverstore";

        private static final String CREATE_TABLE_SERVER = "CREATE TABLE IF NOT EXISTS "
                + SERVER_TABLE_NAME + " (" + BaseColumns._ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT , USER TEXT, PASSWORD TEXT )";

        OpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE_SERVER);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            return;
        }

    }
}
